package com.jd.header;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MP3Activity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imgPlay;
    private SeekBar seekBar;
    private TextView tvTime;

    private boolean isPlay = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        setContentView(R.layout.activity_mp3);
        initView();
        initData();
    }

    private void initView() {
        imgPlay = findViewById(R.id.imgPlay);
        seekBar = findViewById(R.id.seekBar);
        tvTime = findViewById(R.id.tvTime);
    }

    private void initData() {
        imgPlay.setOnClickListener(this);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (MediaPlayerManager.getInstance().getMediaPlayer() != null) {
                    tvTime.setText(MediaPlayerManager.getInstance().postions(MediaPlayerManager.getInstance().getMediaPlayer().getCurrentPosition())
                            + "/" + MediaPlayerManager.getInstance().postions(MediaPlayerManager.getInstance().getMediaPlayer().getDuration()));

                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                MediaPlayerManager.getInstance().pause();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                MediaPlayerManager.getInstance().seekTo(seekBar.getProgress() * 1000);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.imgPlay) {
            if (isPlay) {
                imgPlay.setImageResource(R.mipmap.icon_play_start);
                MediaPlayerManager.getInstance().pause();
                isPlay = false;
            } else {
                imgPlay.setImageResource(R.mipmap.icon_play_end);
                //https://file.zaichengzhang.net/childPlat/Resource/Music/721f3cc0-8e49-11ed-9437-0d3e8f2a6319.undefined
                //https://music.163.com/song/media/outer/url?id=95475.mp3
                MediaPlayerManager.getInstance().start(MP3Activity.this, "https://file.zaichengzhang.net/childPlat/Resource/Music/721f3cc0-8e49-11ed-9437-0d3e8f2a6319.undefined", new MediaPlayerManager.OnPlayLister() {
                    @Override
                    public void onPlaying(int progress, int duration) {
                        int pro = progress / 1000;
                        Log.e("TAG", "播放中 " + pro);
                        seekBar.setProgress(pro);
                        tvTime.setText(MediaPlayerManager.getInstance().postions(progress) + "/" + MediaPlayerManager.getInstance().postions(duration));
                    }

                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        seekBar.setMax(mediaPlayer.getDuration() / 1000);
                    }

                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        mediaPlayer.start();
                        tvTime.setText(MediaPlayerManager.getInstance().postions(mediaPlayer.getDuration()) + "/" + MediaPlayerManager.getInstance().postions(mediaPlayer.getDuration()));
                    }
                });
                isPlay = true;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MediaPlayerManager.getInstance().release();
    }
}